local blaze  = require "blaze"
local app = require "app"
local game = require "club.threecard.sudoku"

local M = game.ui.activity()

M.store_watchers = {
    avatar = {
        ["头像变更"] = function(self, store, state, args)
            self.avatar_icon:set_head(state.head)
        end,
        ["等级变更"] = function(self, store, state, args)
            self.account_level_txt.text = tostring(state.level)
            self.game_level_txt.text = tostring(state.level)
        end
    },
    statistic = {
        ["统计更新"] = function(self, store, state, args)
            self.casual_solved_txt.text = tostring(state:get("休闲-四宫-完成谜题", 0))
            self.casual_min_cost_txt.text = app.i18n.number(state:get("休闲-四宫-最少耗时", 0), "elapsed1")

            self.speed_solved_txt.text = tostring(state:get("火拼-六宫-完成谜题", 0))
            self.speed_min_cost_txt.text = app.i18n.number(state:get("火拼-六宫-最少耗时", 0), "elapsed1")
            self.speed_win_txt.text = tostring(state:get("火拼-六宫-胜利局数", 0))

            self.challenge_solved_txt.text = tostring(state:get("挑战-九宫-完成谜题", 0))
            self.challenge_min_cost_txt.text = app.i18n.number(state:get("挑战-九宫-最少耗时", 0), "elapsed1")
            self.challenge_win_txt.text = tostring(state:get("挑战-九宫-胜利局数", 0))
        end
    }
}

function M:ctor()
    self.res = "club.threecard.sudoku/ui/main.b:Profile"
    local AvatarIcon = require "ui.Common.AvatarIcon"
    self.avatar_icon = AvatarIcon.new()
end

function M:on_injected(bind)
    bind("gameobject.element", self.avatar_icon_obj, self.avatar_icon)
    self.name_txt.text = app.store.avatar.state.name
end

function M:on_set_parent()
    self:super("on_set_parent")
    self.transform:Stretch()
end

function M.before_route_enter(to, from, next)
    next(app.kbe.player().game_statistic_api:query())
end

return M